* Reset settings and initialize log file
launch, path("share/reasons")

*-------------------------------------------------------------------------------
* Price and Wasserman (2024), "The Summer Drop in Female Employment"
*
* Description: Show May-to-July changes in (1) reasons for unemployment and
*              (2) main activity among non-participants.
*-------------------------------------------------------------------------------


* Prepare data and run models
*-------------------------------------------------------------------------------

if "$estimate" != "0" {
	* Load data on adult individuals
	gzuse pid tm month wtfinl tmspline* weeks female youngest unemp nlf whyunemp whynilf using "$basepath/data/derived/cps_bms_sample.dta.gz", clear

	* Restrict to 1994-onward (when NLF activity is known)
	keep if year(dofm(tm)) >= 1994

	* Construct an aggregate category for each sex
	expand = 2
	bysort pid tm: replace youngest = 4 if _n == 2
	label define youngest_lbl 4 "Pooled", add

	* Main activity while out of the labor force
	gen byte retired = (nlf == 1 & whynilf == 1)
	gen byte disabled = (nlf == 1 & whynilf == 2)
	gen byte inschool = (nlf == 1 & whynilf == 3)
	gen byte caretake = (nlf == 1 & whynilf == 4)
	gen byte nlfother = (nlf == 1 & (whynilf == 5 | whynilf == .a))
	assert nlf == retired + disabled + inschool + caretake + nlfother

	* Reason unemployed
	gen byte temploser = (unemp == 1 & whyunemp == 1)
	gen byte permloser = (unemp == 1 & (whyunemp == 2 | whyunemp == 3))
	gen byte leaver = (unemp == 1 & whyunemp == 4)
	gen byte entrant = (unemp == 1 & (whyunemp == 5 | whyunemp == 6))
	assert unemp == temploser + permloser + leaver + entrant

	* Aggregate to the group x year x month level
	gcollapse (mean) nlf caretake inschool disabled retired nlfother unemp temploser permloser leaver entrant (rawsum) wtfinl (first) month tmspline* weeks [pw = wtfinl], by(female youngest tm)
	tempfile core
	save `core'

	* Loop over sex
	foreach f of numlist 0 1 {
		* Loop over parental status
		foreach p of numlist 0 2 4 {
			use if female == `f' & youngest == `p' using `core', clear
			quietly tsset tm

			* Loop over outcome variables
			foreach yvar in nlf caretake inschool disabled retired nlfother unemp temploser permloser leaver entrant {
				* Run the specification
				quietly ivreg2 `yvar' ib5.month tmspline* weeks [aw = wtfinl], bw($bandwidth) robust small
				process_estimates, path("reasons") model("f`f'_p`p'_`yvar'") mayjuly
			}
		}
	}
}


* Prepare estimates
*-------------------------------------------------------------------------------

* Load estimates into memory
load_estimates, path("reasons")


* Plot May-to-July employment changes by household structure
*-------------------------------------------------------------------------------

foreach f of numlist 0 1 {
	foreach p of numlist 0 2 4 {
		* Create a matrix containing the estimates
		matrix coefs_f`f'_p`p' = J(11, 2, .)
		matrix rownames coefs_f`f'_p`p' = "nlf" "caretake" "inschool" "disabled" "retired" "nlfother" "unemp" "temploser" "permloser" "leaver" "entrant"
		matrix colnames coefs_f`f'_p`p' = b se

		* Populate the matrix
		local k = 1
		foreach yvar in nlf caretake inschool disabled retired nlfother unemp temploser permloser leaver entrant {
			estimates restore f`f'_p`p'_`yvar'
			matrix coefs_f`f'_p`p'[`k', 1] =  _b[mayjuly]
			matrix coefs_f`f'_p`p'[`k', 2] = _se[mayjuly]
			local `k++'
		}
	}
}

* Plot the results
foreach f of numlist 0 1 {
	if `f' == 0 {
		local flbl "men"
		local Flbl "Men"
		local col1 "$col1"
		local col2 "$col2"
		local col3 "$col3"
		local mfcolor1
		local mfcolor2
		local mfcolor3
	}
	else if `f' == 1 {
		local flbl "women"
		local Flbl "Women"
		local col1 "gs6"
		local col2 "gs6"
		local col3 "black"
		local mfcolor1 "mfcolor(white)"
		local mfcolor2 "mfcolor(white)"
		local mfcolor3 "mfcolor(black)"
	}

	local legend1 "All prime-age `flbl'"
	local legend2 "`Flbl' without children <18 in the household"
	local legend3 "`Flbl' whose youngest child is age 6 to 12"

	#delimit ;
	coefplot
		(matrix(coefs_f`f'_p4[, 1]), se(coefs_f`f'_p4[, 2]) offset(+.1) msymbol($sym1) mcolor(`col1') `mfcolor1' ciopts(lcolor(`col1')) label("`legend1'"))
		(matrix(coefs_f`f'_p0[, 1]), se(coefs_f`f'_p0[, 2]) offset(0.0) msymbol($sym2) mcolor(`col2') `mfcolor2' ciopts(lcolor(`col2')) label("`legend2'"))
		(matrix(coefs_f`f'_p2[, 1]), se(coefs_f`f'_p2[, 2]) offset(-.1) msymbol($sym3) mcolor(`col3') `mfcolor3' ciopts(lcolor(`col3')) label("`legend3'")),
		title("")
		keep(nlf caretake inschool disabled retired nlfother unemp temploser permloser leaver entrant)
		order(nlf caretake inschool disabled retired nlfother . unemp temploser permloser leaver entrant)
		coeflabels(
			nlf       = "{bf:Not in labor force (1994 on)}"
			caretake  = "   Taking care of house or family"
			inschool  = "   In school"
			disabled  = "   Disabled, ill, or unable to work"
			retired   = "   Retired"
			nlfother  = "   Other or unknown"
			unemp     = "{bf:Unemployed (1994 on)}"
			temploser = "   Temporary layoff"
			permloser = "   Permanent layoff"
			leaver    = "   Job leaver"
			entrant   = "   New entrant or reentrant",
			labgap(-115)
			notick)
		xtitle("Change from May to July (p.p.)")
		xscale(range(-0.5 2))
		xlabel(-0.5(0.5)2.0, format(%5.1f))
		xline(0)
		yscale(alt)
		graphregion(margin(l=100 r=0 b=0 t=0))
		plotregion(margin(l=2 r=2))
		grid(none)
		rescale(100)
		legend(rows(3));
	#delimit cr

	tempfile reasons
	graph save "`reasons'"

	grc1leg "`reasons'", graphregion(margin(zero)) plotregion(margin(zero)) position(5) ring(1)
	nicepdf "$basepath/output/reasons_`flbl'.pdf", xsize(6.0) ysize(3.5) indirect replace
}

* Close the log file
unlaunch
